const babel = require("@babel/core")
const JsxModel = require('plain-design-composition-v2/plugins/babel-plugin-react-model')

module.exports = function JsxModelTransform() {
    return {
        name: 'vite-plugin-react-jsx-model',
        enforce: 'pre',
        transform(src, id) {
            if (/\.([jt])sx?$/.test(id)) {
                const post = babel.transform(src, {
                    filename: id,
                    presets: [
                        "@babel/preset-react",
                        "@babel/preset-typescript"
                    ],
                    plugins: [
                        JsxModel,
                    ],
                }).code
                return {
                    code: post,
                    map: null // 如果可行将提供 source map
                }
            }
        }
    }
}
